package com.hb.znpt.viewmodel;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.hb.znpt.bean.LeaseBean;
import com.hb.znpt.bean.UpgradeBean;
import com.hb.znpt.model.LeaseModel;
import com.hb.znpt.utils.CardJunit;
import com.rxjava.rxlife.RxLife;
import com.unitlib.base.base.BaseViewModel;
import com.unitlib.base.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * @Description: 添加出租信息
 * @Author: houbing
 * @CreateDate: 2021/12/17 0:20
 */
public class LeaseAddViewModel extends BaseViewModel<LeaseModel> {

    public ObservableField<String> name = new ObservableField<>("");
    public ObservableField<String> card = new ObservableField<>("");
    public ObservableField<String> area = new ObservableField<>("");

    public MutableLiveData<String> submitData = new MutableLiveData<>();

    public LeaseAddViewModel(@NonNull Application application) {
        super(application);
        model = new LeaseModel();
    }

    public void submit(View view) {
        if (isEmpty()) {
            submitData.setValue("");
        }
    }

    public void send() {
        model.send(name.get(), card.get(), area.get())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    showDialog();
                })
                .doFinally(() -> {
                    dismissDialog();
                })
                .as(RxLife.as(owner))
                .subscribe(been -> {
                    finish();
                    ToastUtils.showShort("提交成功！");
                }, error -> {
                    ToastUtils.showShort(error.getMessage());
                });
    }

    private boolean isEmpty() {
        if (TextUtils.isEmpty(name.get())) {
            ToastUtils.showShort("请输入承租人姓名！");
            return false;
        }
        if (TextUtils.isEmpty(card.get())) {
            ToastUtils.showShort("请输入承租人身份证！");
            return false;
        }
        if (TextUtils.isEmpty(area.get())) {
            ToastUtils.showShort("请输入承租面积！");
            return false;
        }
        return true;
    }

}
